package xuhaijia.lanou3g.autohome.recommend.originalvideo;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.io.ByteArrayOutputStream;

import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.base.MyImageLoader;
import xuhaijia.lanou3g.autohome.base.BaseFragment;
import xuhaijia.lanou3g.autohome.base.NetTool;
import xuhaijia.lanou3g.autohome.base.URLValues;
import xuhaijia.lanou3g.autohome.db.DBToolForHistory;
import xuhaijia.lanou3g.autohome.myself.History;

/**
 * Created by dllo on 16/8/15.
 */
public class OriVideoFragment extends BaseFragment  {

    private ListView ll;
    private OriVideoAdapter adapter;


    @Override
    protected int setLayout() {
        return R.layout.fragment_listview;
    }

    @Override
    protected void initView() {
        ll = bindView(R.id.lv);
    }

    @Override
    protected void initData() {
        adapter = new OriVideoAdapter(mContext);
        mNetTool.getNetData(URLValues.ORIGINAL_VIDEO_URL, OriVideoBean.class, new NetTool.NetListener<OriVideoBean>() {
            @Override
            public void onSuccess(final OriVideoBean oriVideoBean) {
                adapter.setOriVideoBean(oriVideoBean);
                ll.setAdapter(adapter);
                ll.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                        DBToolForHistory.getInstance().deleteHistoryBy("url = ?" ,
                                oriVideoBean.getResult().getList().get(position).getShareaddress());
                        final History history = new History();
                        history.setUrl(oriVideoBean.getResult().getList().get(position).getShareaddress());
                        history.setTitle(oriVideoBean.getResult().getList().get(position).getTitle());
                        history.setDate(oriVideoBean.getResult().getList().get(position).getTime());
                        history.setLookTime(System.currentTimeMillis());
                        history.setTimes(oriVideoBean.getResult().getList().get(position).getReplycount() + "播放");
                        mNetTool.getBitmap(oriVideoBean.getResult().getList().get(position).getSmallimg(), new NetTool.BitmapListener() {
                            @Override
                            public void getBitmap(Bitmap bitmap) {
                                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                                // 将图片输出的方法
                                // 参数一 输出图片的格式
                                // 参数二 品质 100代表不丢失精度 代表品质是100%
                                // 参数三 输出流
                                bitmap.compress(Bitmap.CompressFormat.PNG , 70 , byteArrayOutputStream);
                                history.setPicture(byteArrayOutputStream.toByteArray());
                                DBToolForHistory.getInstance().insertHistory(history);
                            }
                        });

                        String url =  oriVideoBean.getResult().getList().get(position).getShareaddress();
                        Intent intent = new Intent(getContext() ,VideoPlayerActivity.class);
                        intent.putExtra("url" , url);
                        startActivity(intent);
                    }
                });
            }
            @Override
            public void onError(String errorMsg) {

            }
        });
    }

    @Override
    public void load() {

    }

    @Override
    public void refresh() {

    }



}
